package cn.gdrj.est.service.impl;

import cn.gdrj.est.constants.Constants;
import cn.gdrj.est.constants.MaintainStatusEnum;
import cn.gdrj.est.constants.ResultEnum;
import cn.gdrj.est.dao.MaintainOrderDao;
import cn.gdrj.est.dto.EvaluateInfoDTO;
import cn.gdrj.est.dto.SimpleOrderDTO;
import cn.gdrj.est.entity.Appointment;
import cn.gdrj.est.entity.MaintainOrder;
import cn.gdrj.est.entity.MaintainOrderFlow;
import cn.gdrj.est.entity.Users;
import cn.gdrj.est.exception.BusinessException;
import cn.gdrj.est.request.SubmitOrderRequest;
import cn.gdrj.est.service.AppointmentService;
import cn.gdrj.est.service.EvaluateService;
import cn.gdrj.est.service.MaintainOrderFlowService;
import cn.gdrj.est.service.MaintainOrderService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * 维修订单业务组件
 *
 * @author jun
 * @date 2021/11/10
 */
@Service
public class MaintainOrderServiceImpl implements MaintainOrderService {
    @Autowired
    private MaintainOrderDao maintainOrderDao;
    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private MaintainOrderFlowService maintainOrderFlowService;

    /**
     * 用户提交维修订单
     *
     * @param users
     * @param orderRequest
     * @return
     */
    @Transactional
    @Override
    public SimpleOrderDTO submitOrder(Users users, SubmitOrderRequest orderRequest) {
        // 校验参数
        Long evaluateId = orderRequest.getEvaluateId();
        // 查询评估信息
        EvaluateInfoDTO evaluateInfoDTO = evaluateService.getEvaluateInfoById(evaluateId);
        if(ObjectUtils.isEmpty(evaluateInfoDTO)){
            throw new BusinessException(ResultEnum.FAIL_PARAM);
        }
        // 保存预约信息
        saveAppointment(users,orderRequest);

        // 保存维修订单信息
        MaintainOrder maintainOrder = saveMaintainOrder(users, evaluateInfoDTO);

        // 根据订单id和订单规则生产订单编号（yyyyMMddHHmmss+ 4位随机数 + 10位订单id信息（不够10位前面补0））
        String orderNo = generateOrderNo(maintainOrder);
        maintainOrder.setOrderNo(orderNo);
        // 更新订单编号
        maintainOrderDao.updateOrderNo(maintainOrder.getId(),orderNo);

        // 保存维修订单流水信息
        saveMaintainOrderFlow(users,maintainOrder);

        // 返回订单编号
        SimpleOrderDTO orderDTO = new SimpleOrderDTO();
        orderDTO.setOrderNo(orderNo);
        return orderDTO;
    }

    private void saveMaintainOrderFlow(Users users,MaintainOrder maintainOrder) {
        MaintainOrderFlow maintainOrderFlow = new MaintainOrderFlow();
        maintainOrderFlow.setOrderNo(maintainOrder.getOrderNo());

        // 根据订单的状态获取对应的枚举
        MaintainStatusEnum statusEnum = MaintainStatusEnum.getByCode(maintainOrder.getStatus());
        maintainOrderFlow.setOrderStatus(statusEnum.getCode());
        maintainOrderFlow.setOrderStatusDesc(statusEnum.getMsg());

        maintainOrderFlow.setOperationUId(users.getId());
        maintainOrderFlow.setOperationName(users.getUserName());
        maintainOrderFlow.setCreatedUserId(users.getId());

        maintainOrderFlowService.saveMaintainOrderFlow(maintainOrderFlow);

    }

    private String generateOrderNo(MaintainOrder maintainOrder) {
        StringBuilder builder= new StringBuilder();
        String yyyyMMddHHmmss = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        builder.append(yyyyMMddHHmmss);
        String randomNumbers = RandomUtil.randomNumbers(4);
        builder.append(randomNumbers);
        builder.append(StrUtil.padPre(maintainOrder.getId().toString(),10,'0'));
        return builder.toString();
    }

    private MaintainOrder saveMaintainOrder(Users users, EvaluateInfoDTO evaluateInfoDTO) {
        MaintainOrder maintainOrder = new MaintainOrder();
        // maintainOrder.setOrderNo();
        maintainOrder.setUserId(users.getId());
        maintainOrder.setModelId(evaluateInfoDTO.getModel().getId());
        maintainOrder.setEvaluateId(evaluateInfoDTO.getId());
        maintainOrder.setSubscription(evaluateInfoDTO.getSubscription());
        maintainOrder.setStatus(Constants.Order.MaintainStatus.APPOINT);
        maintainOrder.setCreatedUserId(users.getId());
        maintainOrder.setCreatedTime(new Date());
        maintainOrder.setIsDelete(Constants.EST_NOT);

        maintainOrderDao.insertSelective(maintainOrder);
        return maintainOrder;

    }

    private void saveAppointment(Users users, SubmitOrderRequest orderRequest){
        Appointment appointment = new Appointment();
        appointment.setUserid(users.getId());
        appointment.setPhone(orderRequest.getPhone());
        appointment.setAppintdate(orderRequest.getAppintDate());
        appointment.setTemporalinterval(orderRequest.getTemporalInterval());
        appointment.setAdress(orderRequest.getAdress());
        appointment.setStatus(Constants.Order.AppointStatus.APPOINT);
        appointment.setCreateduserid(users.getId());

        appointmentService.saveAppointment(appointment);
    }


    public static void main(String[] args) {
        System.out.println(StrUtil.padPre("1234",10,"0"));
    }
}
